<template>
  <h2>toRaw 与 markRaw</h2>
  <h3>state:{{ state }}</h3>
  <hr />
  <button @click="testToRaw">测试toRaw</button>
  <button @click="testMarkRaw">测试markRaw</button>
</template>

<script lang='ts'>
import { defineComponent, markRaw, reactive, toRaw } from "vue";
interface UserInfo {
  name: string;
  age: number;
  like?: string[];
}
export default defineComponent({
  name: "App",
  setup() {
    const state = reactive<UserInfo>({
      name: "小明",
      age: 20,
    });
    const testToRaw = () => {
      //toRaw: 得到reactive代理对象的目标数据对象
      //把代理对象变成普通对象，数据变化，界面不变化
      const user = toRaw(state);
      user.name += "==";
      console.log(user, state);
    };
    const testMarkRaw = () => {
      // state.like = ['吃','喝'];
      // state.like[0] += "00";
      const like = ["吃", "喝"];
      state.like = markRaw(like);
      setInterval(() => {
        if (state.like) {
          state.like[0] += "-";//无法修改（不是响应式）
          console.log("setInterval");
        }
      }, 1000);
    };
    return {
      state,
      testToRaw,
      testMarkRaw,
    };
  },
});
</script>
